<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../lib/vue.js"></script>
    <link rel="stylesheet" href="../lib/bootstrap.css">
</head>

<body>
    <div id="app">
        <ul class="list-group">
            <li class="list-group-item active" v-for="item in list" :key="item.id">
                <span class="badge badge-primary">{{item.name}}</span>
                {{item.content}}
            </li>
        </ul>
        <comment-box @helpLoad="load"></comment-box>
    </div>
    <template id="temp">
        <div>
            <div class="form-group">
                <label for="my-input">评论人</label>
                <input id="my-input" class="form-control" type="text" name="name" v-model="name">
                <label for="my-input">内容</label>
                <input id="my-input" class="form-control" type="text" name="content" v-model="content">
                <input type="button" @click="cadd()" class="btn btn-primary" value="发表">
            </div>
        </div>
    </template>
    <script>
        var commentBox = {
            template: "#temp",
            data() {
                return {
                    name:'',
                    content:''
                }
            },
            methods: {
                cadd() {
                    var comment = {id:Date.now(),name:this.name, content:this.content};
                    var list = JSON.parse(localStorage.getItem('cmts')||'[]');
                    list.push(comment);
                    localStorage.setItem('cmts', JSON.stringify(list));
                    this.$emit('helpload');
                }
            }
        }
        var vm = new Vue({
            el: "#app",
            data: {
                list: [
                    { id: 1, name: '李白', content: '天生我才必有用' },
                    { id: 2, name: '杜甫', content: '杜甫的诗好啊' },
                    { id: 3, name: '白居易', content: '白居易也不错' }
                ]
            },
            methods: {
                load(){
                    var list = JSON.parse(localStorage.getItem('cmts')||'[]');
                    this.list = list;
                }
            },
            components: {
                'comment-box':commentBox
            },
            created () {
                this.load();
            }
        })
    </script>
</body>

</html>